package com.jackrain.nea.fc.cp.mapper;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;


/**
 * 服务信息-服务开发
 * Mapper
 *
 * @author xuxinshuai
 * @create 2017/12/14
 */

@Mapper
public interface ViewChrorgMapper {


    /**
     * HR001_ID
     *
     * @param hr001id
     * @return String
     */
    @Select("SELECT count(1) FROM CP_C_HRORG WHERE HR001_ID = #{hr001id}")
    Integer selectorgcount(String hr001id);

    /**
     * 查询ID
     *
     * @param hr001id
     * @return Long
     */
    @Select("SELECT ID FROM CP_C_HRORG WHERE HR001_ID = #{hr001id}")
    Long selectorgId(String hr001id);

    /**
     * HR001_ID
     *
     * @param hr001id
     * @return String
     */
    @Select("SELECT count(1) FROM CP_C_HRORG WHERE CP_C_ORGUP_ID = #{hr001id}")
    Integer selectorgupcount(String hr001id);

    /**
     * 查询ID
     *
     * @param hr001id
     * @return Long
     */
    @Select("SELECT ID FROM CP_C_HRORG WHERE CP_C_ORGUP_ID = #{hr001id}")
    Long selectorgupId(String hr001id);


    /**
     * 查询ECODE
     *
     * @param CODE
     * @return Long
     */

    @Select("SELECT count(1) FROM CP_C_HRORG WHERE ECODE = #{CODE}")
    Integer selectcountcode(String CODE);

    /***
     * 更新组织
     * @param hrorgObj
     * @return int
     */
    @UpdateProvider(type = ViewChrorgMapper.ChrorgSqlProvider.class,
            method = "updateSql")
    int updateHrorg(JSONObject hrorgObj);

    /***
     * 新增组织
     * @param hrorgObj
     * @return int
     */
    @UpdateProvider(type = ViewChrorgMapper.ChrorgSqlProvider.class,
            method = "insertSql")
    int insertHrorg(JSONObject hrorgObj);


    /**
     * HR001_ID
     *
     * @param hr002id
     * @return String
     */
    @Select("SELECT count(1) FROM CP_C_STORE WHERE HR002_ID = #{hr002id}")
    Integer selectStoreCount(String hr002id);

    /**
     * HR001_ID
     *
     * @param hr002id
     * @return String
     */
    @Select("SELECT ID FROM CP_C_STORE WHERE HR002_ID = #{hr002id}")
    List<HashMap> selectStoreId(String hr002id);

    /**
     * HR001_ID
     *
     * @param hr003id
     * @return String
     */
    @Select("SELECT ID FROM CP_C_EMP WHERE HR003_ID = #{hr002id}")
    Long selectEmpId(String hr003id);


    /**
     * HR002_ID
     *
     * @param hr002id
     * @return String
     */
    @Select("SELECT Count(1) FROM CP_C_HRORG WHERE HR002_ID = #{hr002id}")
    Integer selectorgdeptcount(String hr002id);

    /**
     * HR002_ID
     *
     * @param hr002id
     * @return String
     */
    @Select("SELECT ID FROM CP_C_HRORG WHERE HR002_ID = #{hr002id}")
    List<HashMap> selectorgdeptId(String hr002id);

    /***
     * 新增店仓
     * @param hrorgObj
     * @return int
     */
    @UpdateProvider(type = ViewChrorgMapper.ChrorgSqlProvider.class,
            method = "insertStroeSql")
    int insertRstore(JSONObject hrorgObj);

    /***
     * 更新店仓
     * @param hrorgObj
     * @return int
     */
    @UpdateProvider(type = ViewChrorgMapper.ChrorgSqlProvider.class,
            method = "updateStoreSql")
    int updateStore(JSONObject hrorgObj);


    @Slf4j
    class ChrorgSqlProvider {

        public String updateSql(JSONObject map) {


            String sql = new SQL() {
                {
                    UPDATE("CP_C_HRORG");
                    for (String key : map.keySet()) {
                        String id = "ID";
                        if (!id.equals(key)) {
                            SET(key + "= #{" + key + "}");
                        }
                    }
                    WHERE("ID = #{ID}");
                }

            }.toString();
            log.debug("更新组织SQL" + sql);

            return sql;
        }

        public String insertSql(JSONObject jo) {
            String sql = new SQL() {
                {
                    INSERT_INTO("CP_C_HRORG");
                    for (String key : jo.keySet()) {
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
            log.debug("新增组织SQL" + sql);
            return sql;
        }

        public String updateStoreSql(JSONObject map) {
            return new SQL() {
                {
                    UPDATE("CP_C_STORE");
                    for (String key : map.keySet()) {
                        String id = "ID";
                        if (!id.equals(key)) {
                            SET(key + "= #{" + key + "}");
                        }
                    }
                    WHERE("ID = #{ID}");
                }

            }.toString();
        }

        public String insertStroeSql(JSONObject jo) {
            return new SQL() {
                {
                    INSERT_INTO("CP_C_STORE ");
                    for (String key : jo.keySet()) {
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }


    }
}


